package com.pkk.penguin.frame.know.java.driven.jdbc.config;

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * jdbc的配置
 *
 * @author peikunkun
 * @version V1.0
 * @date 2020-10-21 18:02
 **/
@Configuration
public class JdbcConfig {

  /**
   * @return javax.sql.DataSource
   * @Param
   * @author peikunkun
   * @date 2020/10/22 0022 上午 11:48
   * @since
   */
  @Bean
  public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://118.25.123.16:3306/kunzai_test");
    dataSource.setUsername("kunzai_test");
    dataSource.setPassword("kunzaiadmin");
    return dataSource;
  }


  /**
   * Spring封装的操作数据库的工具类
   *
   * @return org.springframework.jdbc.core.JdbcTemplate
   * @Param
   * @author peikunkun
   * @date 2020/10/22 0022 下午 1:10
   * @since
   */
  @Bean
  public JdbcTemplate jdbcTemplate() {
    return new JdbcTemplate(dataSource());
  }

  /**
   * 平台事务管理器
   *
   * @return org.springframework.transaction.PlatformTransactionManager
   * @Param
   * @author peikunkun
   * @date 2020/10/22 0022 下午 1:56
   * @since
   */
  @Bean
  public PlatformTransactionManager platformTransactionManager() {
    return new DataSourceTransactionManager(dataSource());
  }


}
